﻿using Serilog;
using Serilog.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration.Json;
using System;

namespace LPLS.Commons.LogOP
{
    public static class LogHelper
    {
        private static readonly ILogger Logger;

        static LogHelper()
        {
            // 从配置文件加载配置
            var configuration = new ConfigurationBuilder()
                .SetBasePath(AppContext.BaseDirectory) // 设置基础路径
                .AddJsonFile("serilog.json", optional: false, reloadOnChange: true) // 添加 JSON 文件
                .Build();

            var loggerConfiguration = new LoggerConfiguration()
                .ReadFrom.Configuration(configuration);

            Logger = loggerConfiguration.CreateLogger();
        }

        public static void Information(string message)
        {
            Logger.Information(message);
        }

        public static void Error(string message, Exception ex)
        {
            Logger.Error(ex, message);
        }

        public static void Debug(string message)
        {
            Logger.Debug(message);
        }

        public static void Warning(string message)
        {
            Logger.Warning(message);
        }

        public static void CloseAndFlush()
        {
            Log.CloseAndFlush(); // 使用 Serilog.Log 类的 CloseAndFlush 方法
        }
    }
}